#include<stdio.h>
#include<math.h>
#include <stdlib.h>

void function (double gr1, double gr2, double *avg, double *worst);
int main (){
	FILE *output, *grades;
	int ID;
	double gr1, gr2, worst, avg;
	
	grades = fopen("grades.txt", "r");
	
	if (grades == NULL){
		printf("Error: Failed to open grades.txt\n");
      exit(1);
	}	
	
	output = fopen("output.txt", "w");
	
	fprintf(output,"ID          WORST EXAM SCORE          EXAM AVERAGE\n");
	
    while (fscanf(grades, "%d %lf %lf", &ID, &gr1, &gr2) != EOF){
	function (gr1, gr2, &worst, &avg);
	fprintf(output, "  %d            %lf                      %lf\n", ID, avg , worst );
		
	}	
	
	
	
	fclose(grades);
	fclose(output);
	return 0;
	
}


void function (double gr1, double gr2, double *avg, double *worst){
	*avg= (gr1+gr2)/2.0;
	if (gr1<=gr2)
		*worst = gr1;
	else *worst = gr2;
	
}
	

